﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Web;
using System.Data;
using V5_EaspLibs.FrameWork.Model;

namespace V5_FrameWork.Data {
    public class BasePluginHelper {
        private static readonly BasePluginHelper m_Instance = new BasePluginHelper();
        /// <summary>
        /// 
        /// </summary>
        public static BasePluginHelper Instance {
            get { return m_Instance; }
        }
        #region 实体类
        private string _ID;
        /// <summary>
        /// 插件唯一标示
        /// </summary>
        public string ID {
            get { return _ID; }
            set { _ID = value; }
        }
        private string _PluginName;
        /// <summary>
        /// 插件名称
        /// </summary>
        public string PluginName {
            get { return _PluginName; }
            set { _PluginName = value; }
        }
        private string _PluginDesc;
        /// <summary>
        /// 插件说明
        /// </summary>
        public string PluginDesc {
            get { return _PluginDesc; }
            set { _PluginDesc = value; }
        }
        private string _PluginDir;
        /// <summary>
        /// 插件目录
        /// </summary>
        public string PluginDir {
            get { return _PluginDir; }
            set { _PluginDir = value; }
        }
        private string _PluginStatus;
        /// <summary>
        /// 插件状态
        /// </summary>
        public string PluginStatus {
            get { return _PluginStatus; }
            set { _PluginStatus = value; }
        }
        private string _PluginDbType;
        /// <summary>
        /// 数据库类型  OleDb,SqlServer,SQLite
        /// </summary>
        public string PluginDbType {
            get { return _PluginDbType; }
            set { _PluginDbType = value; }
        }

        private string _PluginDbLink;
        /// <summary>
        /// 数据库连接串
        /// </summary>
        public string PluginDbLink {
            get { return _PluginDbLink; }
            set { _PluginDbLink = value; }
        }

        private string _PluginOrder;
        /// <summary>
        /// 插件排序 默认为1
        /// </summary>
        public string PluginOrder {
            get { return _PluginOrder; }
            set { _PluginOrder = value; }
        }
        private string _AddDateTime;
        /// <summary>
        /// 插件添加时间
        /// </summary>
        public string AddDateTime {
            get { return _AddDateTime; }
            set { _AddDateTime = value; }
        }
        #endregion
        #region 后台地址实体类
        /// <summary>
        /// 后台菜单地址实体类
        /// </summary>
        public class PluginMenuNameHelper {
            private int _IsClose = 0;
            /// <summary>
            /// 菜单是否关闭
            /// </summary>
            public int IsClose {
                get { return _IsClose; }
                set { _IsClose = value; }
            }
            private string _Name;
            /// <summary>
            /// 名称
            /// </summary>
            public string Name {
                get { return _Name; }
                set { _Name = value; }
            }
            private string _Type;
            /// <summary>
            /// 类型
            /// </summary>
            public string Type {
                get { return _Type; }
                set { _Type = value; }
            }
            private List<PluginAdminUrlHelper> _ListAdminUrl = new List<PluginAdminUrlHelper>();

            public List<PluginAdminUrlHelper> ListAdminUrl {
                get { return _ListAdminUrl; }
                set { _ListAdminUrl = value; }
            }

        }
        /// <summary>
        /// 后台菜单子项
        /// </summary>
        public class PluginAdminUrlHelper {
            private string _Num;
            /// <summary>
            /// 排序
            /// </summary>
            public string Num {
                get { return _Num; }
                set { _Num = value; }
            }
            private string _Name;
            /// <summary>
            /// 名称
            /// </summary>
            public string Name {
                get { return _Name; }
                set { _Name = value; }
            }
            private string _Href;
            /// <summary>
            /// 地址链接
            /// </summary>
            public string Href {
                get { return _Href; }
                set { _Href = value; }
            }
            private string _Target;
            /// <summary>
            /// 目标
            /// </summary>
            public string Target {
                get { return _Target; }
                set { _Target = value; }
            }
        }
        #endregion
        #region 安装文件记录实体类
        /// <summary>
        /// 安装文件地地址
        /// </summary>
        public class PluginInstallFilesHelper {
            private string _FileUrl;
            /// <summary>
            /// 文件地址
            /// </summary>
            public string FileUrl {
                get { return _FileUrl; }
                set { _FileUrl = value; }
            }
        }
        /// <summary>
        /// 安装文件目录
        /// </summary>
        public class PluginInstallDirsHelper {
            private string _FileDir;
            /// <summary>
            /// 文件目录
            /// </summary>
            public string FileDir {
                get { return _FileDir; }
                set { _FileDir = value; }
            }
        }
        #endregion
        private static string PluginXmlPath = HttpContext.Current.Server.MapPath("~/") + "Config/Plugins.config";
        /// <summary>
        /// 插件
        /// </summary>
        public BasePluginHelper() {
        }
        /// <summary>
        /// 删除插件
        /// </summary>
        /// <param name="guid"></param>
        public void Delete(string guid) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(PluginXmlPath);
            XmlNode xmlNodeRoot = xmlDoc.SelectSingleNode("plugins");
            //检查标签是否存在
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("plugins/plugin[id='" + guid + "']");//查找
            XmlNode xmlNode = xmlNodeList.Item(0);
            xmlNodeRoot.RemoveChild(xmlNode);
            //保存
            xmlDoc.Save(PluginXmlPath);
        }
        /// <summary>
        /// 获取所有插件信息
        /// </summary>
        /// <returns></returns>
        public List<BasePluginHelper> GetAllPlugHelperList() {
            List<BasePluginHelper> list = new List<BasePluginHelper>();
            try {
                XmlDocument xml = new XmlDocument();
                xml.Load(PluginXmlPath);
                XmlNodeList NodesList = xml.SelectNodes("plugins/plugin");
                if (NodesList != null) {
                    BasePluginHelper model;
                    foreach (XmlNode node in NodesList) {
                        try {
                            model = new BasePluginHelper();
                            model.ID = node.SelectSingleNode("id").InnerText;
                            model.PluginName = node.SelectSingleNode("pluginname").InnerText;
                            model.PluginDesc = node.SelectSingleNode("plugindesc").InnerText;
                            model.PluginDir = node.SelectSingleNode("plugindir").InnerText;
                            model.PluginStatus = node.SelectSingleNode("pluginstatus").InnerText;
                            model.PluginDbType = node.SelectSingleNode("plugindbtype").InnerText;
                            model.PluginDbLink = node.SelectSingleNode("plugindblink").InnerText;
                            model.PluginOrder = node.SelectSingleNode("pluginorder").InnerText;
                            model.AddDateTime = node.SelectSingleNode("adddatetime").InnerText;
                            list.Add(model);
                        }
                        catch (Exception ex) {
                            continue;
                        }
                    }
                }
                //排序
                list.Sort(delegate(BasePluginHelper x, BasePluginHelper y) { return x.PluginOrder.CompareTo(y.PluginOrder); });
            }
            catch (Exception ex) {
            }
            return list;
        }
        /// <summary>
        /// 保存插件
        /// </summary>
        /// <param name="model"></param>
        public void Add(BasePluginHelper model) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(PluginXmlPath);
            //检查标签是否存在
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("plugins/plugin[id='" + model.ID + "']");//查找
            XmlNode xmlNode = xmlNodeList.Item(0);
            if (xmlNode != null && !string.IsNullOrEmpty(xmlNode.InnerText)) {
                return;
            }
            Save(xmlDoc, model);
        }
        /// <summary>
        /// 修改插件
        /// </summary>
        /// <param name="model"></param>
        public void Update(BasePluginHelper model) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(PluginXmlPath);
            //检查标签是否存在
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("plugins/plugin[id='" + model.ID + "']");//查找
            XmlNode xmlNode = xmlNodeList.Item(0);
            if (xmlNode != null && !string.IsNullOrEmpty(xmlNode.InnerText)) {
                xmlNode.SelectSingleNode("id").InnerText = model.ID;
                xmlNode.SelectSingleNode("pluginname").InnerText = model.PluginName;
                xmlNode.SelectSingleNode("plugindesc").InnerText = model.PluginDesc;
                xmlNode.SelectSingleNode("plugindir").InnerText = model.PluginDir;
                xmlNode.SelectSingleNode("pluginstatus").InnerText = model.PluginStatus;
                xmlNode.SelectSingleNode("plugindbtype").InnerText = model.PluginDbType;
                xmlNode.SelectSingleNode("plugindblink").InnerText = model.PluginDbLink;
                xmlNode.SelectSingleNode("pluginorder").InnerText = model.PluginOrder;
                xmlNode.SelectSingleNode("adddatetime").InnerText = model.AddDateTime;
                xmlDoc.Save(PluginXmlPath);
            }
        }
        /// <summary>
        /// 保存插件
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <param name="model"></param>
        private void Save(XmlDocument xmlDoc, BasePluginHelper model) {
            XmlElement newElement = xmlDoc.CreateElement("plugin");
            XmlElement elID = xmlDoc.CreateElement("id");
            XmlElement elPluginName = xmlDoc.CreateElement("pluginname");
            XmlElement elPluginDesc = xmlDoc.CreateElement("plugindesc");
            XmlElement elPluginDir = xmlDoc.CreateElement("plugindir");
            XmlElement elPluginStatus = xmlDoc.CreateElement("pluginstatus");
            XmlElement elPluginDbType = xmlDoc.CreateElement("plugindbtype");
            XmlElement elPluginDbLink = xmlDoc.CreateElement("plugindblink");
            XmlElement elPluginOrder = xmlDoc.CreateElement("pluginorder");
            XmlElement elAddDateTime = xmlDoc.CreateElement("adddatetime");
            elID.InnerText = model.ID;
            elPluginName.InnerText = model.PluginName;
            elPluginDesc.InnerText = model.PluginDesc;
            elPluginDir.InnerText = model.PluginDir;
            elPluginStatus.InnerText = model.PluginStatus;
            elPluginDbType.InnerText = model.PluginDbType;
            elPluginDbLink.InnerText = model.PluginDbLink;
            elPluginOrder.InnerText = model.PluginOrder;
            elAddDateTime.InnerText = model.AddDateTime;
            newElement.AppendChild(elID);
            newElement.AppendChild(elPluginName);
            newElement.AppendChild(elPluginDesc);
            newElement.AppendChild(elPluginDir);
            newElement.AppendChild(elPluginStatus);
            newElement.AppendChild(elPluginDbType);
            newElement.AppendChild(elPluginDbLink);
            newElement.AppendChild(elPluginOrder);
            newElement.AppendChild(elAddDateTime);
            xmlDoc.DocumentElement.AppendChild(newElement);
            xmlDoc.Save(PluginXmlPath);
        }
        /// <summary>
        /// 检查插件是否存在
        /// </summary>
        /// <param name="PluginDir"></param>
        /// <returns></returns>
        public bool Exists(string PluginDir) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(PluginXmlPath);
            //检查标签是否存在
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("plugins/plugin[plugindir='" + PluginDir + "']");//查找
            XmlNode xmlNode = xmlNodeList.Item(0);
            if (xmlNode != null && !string.IsNullOrEmpty(xmlNode.InnerText)) {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取插件模型 Plugin.config
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public BasePluginHelper GetModel(string ID) {
            XmlDocument xml = new XmlDocument();
            xml.Load(PluginXmlPath);
            XmlNodeList NodesList = xml.SelectNodes("plugins/plugin[id='" + ID + "']");
            XmlNode node = NodesList.Item(0);
            if (node != null) {
                BasePluginHelper model = new BasePluginHelper();
                model.ID = node.SelectSingleNode("id").InnerText;
                model.PluginName = node.SelectSingleNode("pluginname").InnerText;
                model.PluginDesc = node.SelectSingleNode("plugindesc").InnerText;
                model.PluginDir = node.SelectSingleNode("plugindir").InnerText;
                model.PluginStatus = node.SelectSingleNode("pluginstatus").InnerText;
                model.PluginDbType = node.SelectSingleNode("plugindbtype").InnerText;
                model.PluginDbLink = node.SelectSingleNode("plugindblink").InnerText;
                model.PluginOrder = node.SelectSingleNode("pluginorder").InnerText;
                model.AddDateTime = node.SelectSingleNode("adddatetime").InnerText;
                //记载后台菜单
                //加载生成静态地址
                //记录安装文件地址
                //
                return model;
            }
            return null;
        }

        /// <summary>
        /// 获取后台菜单集合
        /// </summary>
        /// <param name="PluginDirPath"></param>
        /// <returns></returns>
        public List<PluginMenuNameHelper> GetAdminMenuList(string PluginDirPath) {
            List<PluginMenuNameHelper> ListPluginAdinUrl = new List<PluginMenuNameHelper>();
            PluginDirPath = HttpContext.Current.Server.MapPath("~/") + "Admin/" + PluginDirPath + "/Plugin.config";
            if (!File.Exists(PluginDirPath)) {
                return ListPluginAdinUrl;
            }
            XmlDocument xml = new XmlDocument();
            xml.Load(PluginDirPath);
            XmlNodeList nodeList = xml.SelectNodes("plugin/urls");
            foreach (XmlNode n in nodeList) {
                PluginMenuNameHelper PluginMenuName = new PluginMenuNameHelper();
                PluginMenuName.Name = n.Attributes["name"].Value;
                PluginMenuName.Type = n.Attributes["type"].Value;
                if (n.Attributes["isclose"] != null) {
                    PluginMenuName.IsClose = int.Parse("0" + n.Attributes["isclose"].Value);
                }
                else {
                    PluginMenuName.IsClose = 0;
                }
                XmlNodeList nodeListChild = n.SelectNodes("url");
                foreach (XmlNode nc in nodeListChild) {
                    PluginAdminUrlHelper model = new PluginAdminUrlHelper();
                    model.Num = nc.Attributes["num"].Value;
                    model.Name = nc.Attributes["name"].Value;
                    model.Href = nc.Attributes["href"].Value;
                    model.Target = nc.Attributes["target"].Value;
                    PluginMenuName.ListAdminUrl.Add(model);
                }
                ListPluginAdinUrl.Add(PluginMenuName);
            }
            return ListPluginAdinUrl;
        }
        /// <summary>
        /// 获取生成静态地址
        /// </summary>
        /// <param name="PluginDirPath"></param>
        /// <returns></returns>
        public List<PluginMenuNameHelper> GetAdminHtmlList(string PluginDirPath) {
            List<PluginMenuNameHelper> ListPluginAdinUrl = new List<PluginMenuNameHelper>();
            PluginDirPath = HttpContext.Current.Server.MapPath("~/") + "Admin/" + PluginDirPath + "/Plugin.config";
            XmlDocument xml = new XmlDocument();
            xml.Load(PluginDirPath);
            XmlNodeList nodeList = xml.SelectNodes("plugin/htmls");
            foreach (XmlNode n in nodeList) {
                PluginMenuNameHelper PluginMenuName = new PluginMenuNameHelper();
                PluginMenuName.Name = n.Attributes["name"].Value;
                PluginMenuName.Type = n.Attributes["type"].Value;
                XmlNodeList nodeListChild = n.SelectNodes("html");
                foreach (XmlNode nc in nodeListChild) {
                    PluginAdminUrlHelper model = new PluginAdminUrlHelper();
                    model.Num = nc.Attributes["num"].Value;
                    model.Name = nc.Attributes["name"].Value;
                    model.Href = nc.Attributes["href"].Value;
                    model.Target = nc.Attributes["target"].Value;
                    PluginMenuName.ListAdminUrl.Add(model);
                }
                ListPluginAdinUrl.Add(PluginMenuName);
            }
            return ListPluginAdinUrl;
        }
        /// <summary>
        /// 获取安装文件
        /// </summary>
        /// <param name="PluginDirPath"></param>
        /// <returns></returns>
        private List<PluginInstallFilesHelper> GetInstallFilesList(string PluginDirPath) {
            List<PluginInstallFilesHelper> ListPluginAdinUrl = new List<PluginInstallFilesHelper>();
            PluginDirPath = HttpContext.Current.Server.MapPath("~/") + "Admin/" + PluginDirPath + "/Plugin.config";
            XmlDocument xml = new XmlDocument();
            xml.Load(PluginDirPath);
            XmlNodeList nodeList = xml.SelectNodes("plugin/htmls");
            foreach (XmlNode n in nodeList) {
                PluginInstallFilesHelper model = new PluginInstallFilesHelper();
                model.FileUrl = n.SelectSingleNode("file").InnerText;
            }
            return ListPluginAdinUrl;
        }
        /// <summary>
        /// 获取安装目录
        /// </summary>
        /// <param name="PluginDirPath"></param>
        /// <returns></returns>
        private List<PluginInstallDirsHelper> GetInstallDirsList(string PluginDirPath) {
            List<PluginInstallDirsHelper> ListPluginAdinUrl = new List<PluginInstallDirsHelper>();
            PluginDirPath = HttpContext.Current.Server.MapPath("~/") + "Admin/" + PluginDirPath + "/Plugin.config";
            XmlDocument xml = new XmlDocument();
            xml.Load(PluginDirPath);
            XmlNodeList nodeList = xml.SelectNodes("plugin/htmls");
            foreach (XmlNode n in nodeList) {
                PluginInstallDirsHelper model = new PluginInstallDirsHelper();
                model.FileDir = n.SelectSingleNode("dir").InnerText;
            }
            return ListPluginAdinUrl;
        }
    }
}
